package com.itheima.leyou.queue;

import com.itheima.leyou.service.StorageService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class StorageQueue {

    @Resource
    StorageService storageService;

    @RabbitListener(queues = "storage_queue")
    public void getStorageQueue(String msg){
        //1、接收消息并输出
        System.out.println("storage_queue接收消息："+msg);

        //msg:需要sku_id

        //2、调用storageService的处理库存方法insertStorage
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            resultMap = storageService.insertStorage(msg, 0, 1);

            //3、当消息处理有误时，输出
            if (!(Boolean) resultMap.get("result")){
                System.out.println("storage_queue消息处理失败："+resultMap.get("msg").toString());
            }

        }catch (Exception e){
            System.out.println("storage_queue消息处理失败："+e.getMessage());
        }

        //4、消息处理完成时，输出
        System.out.println("storage_queue消息处理完毕："+resultMap);
    }
}
